package com.zlx.sharelive.activity;

import android.app.Dialog;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.BaseBean;
import com.zlx.module_base.base_api.res_data.DeleteFocusedBean;
import com.zlx.module_base.base_api.res_data.FocusedBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;
import com.zlx.sharelive.utils.GlideImgManager;
import com.zlx.sharelive.utils.StringUt;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的关注
 */
public class MyFollowAc extends BaseAc {

    @BindView(R.id.rv_data)
    RecyclerView rv_data;

    private int page = 1;
    private List<FocusedBean.InfoDTO.RecordsDTO> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<FocusedBean.InfoDTO.RecordsDTO> adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_my_follow;
    }

    @Override
    public void initViews() {
        super.initViews();

        page = 1;
        getFocusedList(page);
        initRecyclerView();
    }

    /**
     * 我的关注
     */
    private void getFocusedList(int page) {
        ApiUtil.getUserApi().getFocusedList(SPUtil.getToken(this), String.valueOf(page))
                .observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<FocusedBean>>() {
                    @Override
                    public void onSuccess(ApiResponse<FocusedBean> data) {
                        mList.clear();
                        if (null != data) {
                            if (null != data.getData()) {
                                if (data.getData().getCode() == 0) {
                                    if (null != data.getData().getInfo()) {
                                        if (data.getData().getInfo().size() > 0) {
                                            if (null != data.getData().getInfo().get(0).getRecords()) {
                                                mList.addAll(data.getData().getInfo().get(0).getRecords());
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();

                    }
                }));
    }

    /**
     * 初始化列表
     */
    private void initRecyclerView() {
        rv_data.setLayoutManager(new LinearLayoutManager(this));

        adapter = new CommonRecyclerViewAdapter<FocusedBean.InfoDTO.RecordsDTO>(this, mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, FocusedBean.InfoDTO.RecordsDTO entity, int position) {
                ImageView iv_follow_head = holder.getView(R.id.iv_follow_head);
                GlideImgManager.glideLoader(MyFollowAc.this, entity.getAvatar(), iv_follow_head, 0);
                holder.setText(R.id.tv_follow_nickname, entity.getNickName());
                holder.setText(R.id.tv_time, entity.getBirthday());
                ImageView iv_vip = holder.getView(R.id.iv_vip);
                ImageView iv_goddess = holder.getView(R.id.iv_goddess);
                if (entity.getAuthentication().equals("2")) {
                    iv_goddess.setVisibility(View.GONE);
                } else {
                    iv_goddess.setVisibility(View.VISIBLE);
                }
                if (!StringUt.isEmpty(entity.getIsvip())) {
                    if (entity.getIsvip().equals("2")) {
                        iv_vip.setVisibility(View.GONE);
                    } else {
                        iv_vip.setVisibility(View.VISIBLE);
                    }
                }
                if (entity.getMutualFocused().equals("1")) {
                    holder.setText(R.id.tv_attention, "互相关注");
                } else {
                    holder.setText(R.id.tv_attention, "已关注");
                }
                TextView tv_attention = holder.getView(R.id.tv_attention);

                tv_attention.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        addorDeleteFocused(entity.getId(), entity.getMutualFocused());
                    }
                });

            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_follow;
            }
        };

        rv_data.setAdapter(adapter);

        adapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                if (!mList.get(position).getMutualFocused().equals("1")) {
                    //弹出框
                    dialog(position);
                }

            }
        });
    }

    private void addorDeleteFocused(String id, String type) {
        if (!StringUt.isEmpty(type)) {
            if (!type.equals("2")) {
                dialog(id);
            } else {
                DeleteFocusedBean deleteFocusedBean = new DeleteFocusedBean();
                deleteFocusedBean.setUid(id);
                ApiUtil.getUserApi().addorDeleteFocused(SPUtil.getToken(this),
                        deleteFocusedBean).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<BaseBean>>() {
                    @Override
                    public void onSuccess(ApiResponse<BaseBean> data) {
                        if (null != data) {
                            if (null != data.getData()) {
                                if (data.getData().getCode() == 0) {
                                    page = 1;
                                    getFocusedList(page);
                                } else {
                                    Toast.makeText(MyFollowAc.this, data.getData().getMsg(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    }
                }));
            }
        }
    }


    private void dialog(String id) {
        //1、使用Dialog、设置style
        final Dialog dialog = new Dialog(MyFollowAc.this, R.style.DialogTheme);
        //2、设置布局
        View view = View.inflate(MyFollowAc.this, R.layout.dialog_cancel, null);
        dialog.setContentView(view);

        TextView tv_text = view.findViewById(R.id.tv_text);

        tv_text.setText("是否取消关注");

        TextView tv_cancel = view.findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        TextView tv_determine = view.findViewById(R.id.tv_determine);
        tv_determine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                DeleteFocusedBean deleteFocusedBean = new DeleteFocusedBean();
                deleteFocusedBean.setUid(id);
                ApiUtil.getUserApi().addorDeleteFocused(SPUtil.getToken(MyFollowAc.this),
                        deleteFocusedBean).observe(MyFollowAc.this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<BaseBean>>() {
                    @Override
                    public void onSuccess(ApiResponse<BaseBean> data) {
                        dialog.dismiss();
                        if (null != data) {
                            if (null != data.getData()) {
                                if (data.getData().getCode() == 0) {
                                    page = 1;
                                    getFocusedList(page);
                                } else {
                                    Toast.makeText(MyFollowAc.this, data.getData().getMsg(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    }
                }));
            }
        });

        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.CENTER);
        //设置弹出动画
//        window.setWindowAnimations(R.style.main_menu_animStyle);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

    }

    private void dialog(int position) {
        //1、使用Dialog、设置style
        final Dialog dialog = new Dialog(MyFollowAc.this, R.style.DialogTheme);
        //2、设置布局
        View view = View.inflate(MyFollowAc.this, R.layout.dialog_cancel, null);
        dialog.setContentView(view);

        TextView tv_text = view.findViewById(R.id.tv_text);

        tv_text.setText("是否取消关注");

        TextView tv_cancel = view.findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        TextView tv_determine = view.findViewById(R.id.tv_determine);
        tv_determine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                DeleteFocusedBean deleteFocusedBean = new DeleteFocusedBean();
                deleteFocusedBean.setUid(mList.get(position).getId());
                ApiUtil.getUserApi().addorDeleteFocused(SPUtil.getToken(MyFollowAc.this),
                        deleteFocusedBean).observe(MyFollowAc.this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<BaseBean>>() {
                    @Override
                    public void onSuccess(ApiResponse<BaseBean> data) {
                        page = 1;
                        getFocusedList(page);
                    }
                }));
            }
        });

        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.CENTER);
        //设置弹出动画
//        window.setWindowAnimations(R.style.main_menu_animStyle);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

    }


    @OnClick(R.id.iv_back)
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

}
